package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.FeePayHistory;
import cn.net.shuxing.yifu.common.bean.UserTransfer;
import cn.net.shuxing.yifu.common.mapper.FeePayHistoryMapper;
import cn.net.shuxing.yifu.common.util.LocalDateTimeUtil;
import cn.net.shuxing.yifu.common.util.TimeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

@Service
public class FeePayHistoryService extends HistoryService<FeePayHistory, FeePayHistoryMapper> {
    @Autowired
    protected UserRealInfoService userRealInfoService;

    @Autowired
    protected CoinTypeService coinTypeService;

    public FeePayHistory insert(Integer userId, Integer dealScene, Integer coinTypeId, BigDecimal feeCount) {
        FeePayHistory feePayHistory = new FeePayHistory();
        feePayHistory.setUserId(userId);
        feePayHistory.setDealScene(dealScene);
        feePayHistory.setCoinTypeId(coinTypeId);
        feePayHistory.setFeeCount(feeCount);
        insert(feePayHistory);
        return getByPrimaryKey(feePayHistory.getId());
    }
    public BigDecimal getTotal(){
        List<FeePayHistory> all = getAll();
        BigDecimal total = new BigDecimal(0);
        for (FeePayHistory feePayHistory : all) {
            total = total.add(feePayHistory.getFeeCount());
        }
        return total;
    }
    public BigDecimal getMonth(){
            TimeExample example = new TimeExample(FeePayHistory.class);
            Example.Criteria criteria = example.getCriteria();
            criteria.andGreaterThanOrEqualTo("createTime", LocalDateTimeUtil.getThisMonthFirstDay());
        List<FeePayHistory> feePayHistories = getsByExample(example);
        BigDecimal month = new BigDecimal(0);
        for (FeePayHistory feePayHistory : feePayHistories) {
             month = month.add(feePayHistory.getFeeCount());
        }
        return month;
    }

    @Override
    protected void complete(FeePayHistory bean) {
        Integer coinTypeId = bean.getCoinTypeId();
        String name = coinTypeService.getByPrimaryKey(coinTypeId).getName();
        bean.setCoinTypeName(name);
        Integer userId = bean.getUserId();
        String realName = userRealInfoService.getByPrimaryKey(userId).getRealName();
        bean.setUserRealName(realName);
    }
}
